package com.wu.project.admin.api;

import com.wu.project.admin.annotations.Authorization;
import com.wu.project.admin.annotations.Log;
import com.wu.project.admin.component.JsonResult;
import com.wu.project.admin.entity.*;
import com.wu.project.admin.service.GoodsService;
import com.wu.project.admin.util.CommonResultUtil;
import jxl.write.WriteException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 50816
 */
@RestController
@RequestMapping("/goods")
public class GoodsApi {

    @Autowired
    GoodsService goodsService;

    /**
     * 获取列表
     */
    @Authorization
    @Log(name = "商品列表")
    @PostMapping("/list")
    public JsonResult goodsList(@RequestBody GoodsQueryVO goodsQueryVO) {
        return goodsService.getGoods(goodsQueryVO);
    }

    /**
     * 下架商品
     */
    @Authorization
    @Log(name = "下架商品")
    @PostMapping("/down")
    public JsonResult goodsOffShelf(@RequestBody GoodsQueryVO goodsQueryVO) {
        return goodsService.goodsDown(goodsQueryVO);
    }

    @Authorization
    @Log(name = "下载导入模版")
    @GetMapping("/download/xls")
    public void downloadXlsDemo(HttpServletResponse response) throws IOException, WriteException {
        goodsService.downloadXlsDemo(response);
    }

    /**
     * 添加商品
     */
    @Authorization
    @Log(name = "批量导入商品")
    @PostMapping("/up")
    public JsonResult goodsOnShelf(@RequestParam("file") MultipartFile file) {
        goodsService.uploadXls(file);
        return CommonResultUtil.Instance.success();
    }

    @Authorization
    @Log(name = "更新商品信息")
    @PostMapping("/update")
    public JsonResult goodsUpdate(@RequestBody GoodsDTO goodsDTO) {
        return goodsService.updateGoods(goodsDTO);
    }

    @Authorization
    @Log(name = "查询商品一级分类")
    @GetMapping("/query/type1")
    public JsonResult type1() {
        return goodsService.queryType1();
    }

    @Authorization
    @Log(name = "新增商品一级分类")
    @PostMapping("/add/one")
    public JsonResult addOneType(@RequestParam String name) {
        return goodsService.addTypeOne(name);
    }

    @Authorization
    @Log(name = "更新商品一级分类")
    @PostMapping("/update/one")
    public JsonResult updateOneType(@RequestBody GoodsTypeOne goodsTypeOne) {
        return goodsService.updateTypeOne(goodsTypeOne);
    }

    @Authorization
    @Log(name = "删除商品一级分类")
    @PostMapping("/delete/one")
    public JsonResult deleteOneType(@RequestBody GoodsTypeOne goodsTypeOne) {
        return goodsService.deleteTypeOne(goodsTypeOne);
    }

    @Authorization
    @Log(name = "查询商品二级分类")
    @GetMapping("/query/type2")
    public JsonResult type2(Integer oneId) {
        return goodsService.queryType2(oneId);
    }

    @Authorization
    @Log(name = "添加商品二级分类")
    @PostMapping("/add/two")
    public JsonResult addTwoType(@RequestParam String name, @RequestParam Long oneId) {
        return goodsService.addTypeTwo(name, oneId);
    }

    @Authorization
    @Log(name = "更新商品二级分类")
    @PostMapping("/update/two")
    public JsonResult updateOneType(@RequestBody GoodsTypeTwo goodsTypeTwo) {
        return goodsService.updateTypeTwo(goodsTypeTwo);
    }

    @Authorization
    @Log(name = "删除商品二级分类")
    @PostMapping("/delete/two")
    public JsonResult deleteOneType(@RequestBody GoodsTypeTwo goodsTypeTwo) {
        return goodsService.deleteTypeTwo(goodsTypeTwo);
    }

    @Authorization
    @Log(name = "查询全部的二级分类")
    @GetMapping("/two/all")
    public JsonResult getTwoAllData() {
        return goodsService.getTwoAllData();
    }

    @Authorization
    @Log(name = "查询商品三级分类")
    @GetMapping("/query/type3")
    public JsonResult getThreeList(Integer twoId) {
        return goodsService.getThreeGoodsList(twoId);
    }

    @Authorization
    @Log(name = "新增商品三级分类")
    @PostMapping("/add/three")
    public JsonResult addThreeType(@RequestParam String name, @RequestParam Long twoId) {
        return goodsService.addTypeThree(name, twoId);
    }

    @Authorization
    @Log(name = "删除商品三级分类")
    @PostMapping("/delete/three")
    public JsonResult deleteThreeType(@RequestBody ShopThreeDTO shopThreeDTO) {
        return goodsService.deleteTypeThree(shopThreeDTO);
    }

    @Authorization
    @Log(name = "更新商品三级分类")
    @PostMapping("/update/three")
    public JsonResult updateThreeType(@RequestBody ShopThreeDTO shopThreeDTO) {
        return goodsService.updateTypeThree(shopThreeDTO);
    }

    @Authorization
    @Log(name = "查询所有的商品三级分类")
    @GetMapping("/queryAll/three")
    public JsonResult queryThreeAll() {
        return goodsService.queryAllThree();
    }


}
